package com.huilian.iotbox.admin.controller;

import com.huilian.iotbox.data.controller.BaseController;
import com.huilian.iotbox.data.properties.FileProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;

/**
 * @author: zhangyingjie
 * @description:
 * @date: 2018/6/4 17:33
 */
@RestController
@RequestMapping("/api/fileExport")
public class FileExportController extends BaseController {

    @Autowired
    private FileProperties fileProperties;

    //文件下载
    @PostMapping("/download/{fileName:.+}")
    public void download(HttpServletResponse response, @PathVariable("fileName") String fileName, HttpServletRequest request) {
        File file = null;
        OutputStream os = null;
        FileInputStream inputStream = null;
        try {
            file = new File(fileProperties.getSaveImgPath(), fileName);
            if (file.exists()) {
                response.setContentType("application/octet-stream");// 设置强制下载不打开
                response.setHeader("Pragma", "public");
                response.setCharacterEncoding("UTF-8");
                String newName = file.getName();
                if (request.getHeader("USER-AGENT").toLowerCase().indexOf("firefox") >= 0) {
                    newName = new String(newName.getBytes("UTF-8"), "ISO-8859-1");
                } else {
                    newName = URLEncoder.encode(newName, "UTF-8");
                }
                response.addHeader("Content-Disposition","attachment;filename=\"" + newName + "\"");
                try {
                    inputStream = new FileInputStream(file);
                    os = response.getOutputStream();
                    byte[] b = new byte[1024];
                    int length;
                    while ((length = inputStream.read(b)) > 0) {
                        os.write(b, 0, length);
                    }
                    os.flush();

                } catch (IOException ex) {
                    ex.printStackTrace();
                } finally {
                    if (inputStream != null) {
                        inputStream.close();
                        inputStream = null;
                    }
                    if (os != null) {
                        os.close();
                        os = null;
                    }
                    // 删除创建的新文件
                    this.deleteFile(file);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 下载成功后删除
     *
     * @param files
     */
    private void deleteFile(File... files) {
        for (File file : files) {
            if (file.exists()) {
                file.delete();
            }
        }
    }




}
